<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.gridprotectionalliance.oam</groupId>
        <artifactId>oam_2.10.0-M7</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>oam-assembly</artifactId>
    <packaging>pom</packaging>


    <build>

        <resources>
            <resource>
                <directory>${project.basedir}/src/main/filtered-resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>

        <plugins>

            <!-- The default memory allocations for karaf are insufficient, override them only touching the memory options -->
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>maven-replacer-plugin</artifactId>
                <version>1.3.9</version>
                <executions>
                    <execution>
                        <id>fix-win-memory</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                        <configuration>
                            <ignoreErrors>false</ignoreErrors>
                            <outputFile>
                                target/dependencies/win/apache-karaf-${karaf.version}/bin/karaf.bat
                            </outputFile>
                            <includes>
                                <include>target/dependencies/win/apache-karaf-${karaf.version}/bin/karaf.bat</include>
                            </includes>
                            <replacements>
                                <replacement>
                                    <token>JAVA_MAX_MEM=512M</token>
                                    <value>JAVA_MAX_MEM=1024M</value>
                                </replacement>
                                <replacement>
                                    <token>JAVA_MAX_PERM_MEM=64M</token>
                                    <value>JAVA_MAX_PERM_MEM=256M</value>
                                </replacement>
                            </replacements>
                        </configuration>
                    </execution>
                    <execution>
                        <id>fix-unix-memory</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                        <configuration>
                            <ignoreErrors>false</ignoreErrors>
                            <outputFile>
                                target/dependencies/unix/apache-karaf-${karaf.version}/bin/karaf
                            </outputFile>
                            <includes>
                                <include>target/dependencies/unix/apache-karaf-${karaf.version}/bin/karaf</include>
                            </includes>
                            <replacements>
                                <replacement>
                                    <token>JAVA_MAX_MEM=512M</token>
                                    <value>JAVA_MAX_MEM=1024M</value>
                                </replacement>
                                <replacement>
                                    <token>JAVA_MAX_PERM_MEM=64M</token>
                                    <value>JAVA_MAX_PERM_MEM=256M</value>
                                </replacement>
                            </replacements>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- together with the resources section above, this copies over the feature files and default configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>filter</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Gets karaf distribution and unpacks it into target directory for windows/linux -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>

                <executions>
                    <execution>
                        <id>unpack-unix</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.karaf</groupId>
                                    <artifactId>apache-karaf</artifactId>
                                    <version>${karaf.version}</version>
                                    <type>tar.gz</type>
                                    <outputDirectory>target/dependencies/unix</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>

                    <execution>
                        <id>unpack-win</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.karaf</groupId>
                                    <artifactId>apache-karaf</artifactId>
                                    <version>${karaf.version}</version>
                                    <type>zip</type>
                                    <outputDirectory>target/dependencies/win</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Creates assembled distributions for windows/linux -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven-assembly-plugin.version}</version>
                <executions>
                    <execution>
                        <id>unix-bin</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/descriptors/unix-bin.xml</descriptor>
                            </descriptors>
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <tarLongFileMode>gnu</tarLongFileMode>
                            <archiverConfig>
                                <duplicateBehavior>skip</duplicateBehavior>
                            </archiverConfig>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                    <execution>
                        <id>windows-bin</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/descriptors/windows-bin.xml</descriptor>
                            </descriptors>
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <archiverConfig>
                                <duplicateBehavior>skip</duplicateBehavior>
                            </archiverConfig>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>dist</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.karaf.tooling</groupId>
                        <artifactId>features-maven-plugin</artifactId>
                        <version>${karaf.version}</version>

                        <executions>
                            <execution>
                                <id>add-bundles-to-repo</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>add-features-to-repo</goal>
                                </goals>

                                <configuration>
                                    <descriptors>
                                        <descriptor>file:${basedir}/target/classes/oam-feature.xml</descriptor>
                                    </descriptors>

                                    <features>
                                        <feature>oam-lib</feature>
                                        <feature>oam-server</feature>
                                    </features>
                                    <includeMvnBasedDescriptors>true</includeMvnBasedDescriptors>
                                    <repository>target/oam-repo</repository>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

    </profiles>

    <dependencies>

        <!-- Dynamic OSGi components -->

        <dependency>
            <groupId>org.gridprotectionalliance.oam</groupId>
            <artifactId>archivist-adapter_${scala.version}</artifactId>
            <version>${oam.version}</version>
        </dependency>

        <dependency>
            <groupId>org.gridprotectionalliance.oam</groupId>
            <artifactId>oam-db-service_${scala.version}</artifactId>
            <version>${oam.version}</version>
        </dependency>

    </dependencies>

</project>